package com.example.weatherforcast;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity implements
		android.view.View.OnClickListener, OnCheckedChangeListener {
	Button btn_login1, btn_register1;
	EditText et_name, et_password, et_password2;
	
	RadioButton rb_login1, rb_login2;
	LinearLayout ll;
	SharedPreferences mpreferences;
	Editor editor;
	private String name, pass1, pass2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		initview();
		initData();
		listen();
	}

	private void initview() {
		// TODO Auto-generated method stub
		btn_login1 = (Button) findViewById(R.id.btn_login1);
		btn_register1 = (Button) findViewById(R.id.btn_register1);
		et_name = (EditText) findViewById(R.id.et_name);
		et_password = (EditText) findViewById(R.id.et_password);
		et_password2 = (EditText) findViewById(R.id.et_password2);
		rb_login1 = (RadioButton) findViewById(R.id.rb_one);
		rb_login2 = (RadioButton) findViewById(R.id.rb_two);
		ll = (LinearLayout) findViewById(R.id.ll_login);

	}

	private void listen() {
		// TODO Auto-generated method stub
		btn_login1.setOnClickListener(this);
		btn_register1.setOnClickListener(this);
		rb_login1.setOnCheckedChangeListener(this);
		rb_login2.setOnCheckedChangeListener(this);

	}

	private void initData() {
		// TODO Auto-generated method stub
		mpreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
		editor = mpreferences.edit();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.rb_one:
				ll.setVisibility(View.GONE);
				break;
			case R.id.rb_two:
				ll.setVisibility(View.VISIBLE);
				break;

			default:
				break;
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_login1:
			name = et_name.getText().toString();
			pass1 = et_password.getText().toString();
if ((name.equals(mpreferences.getString("name", "1")))&&(pass1.equals(mpreferences.getString("pass1", "1")))) {
	Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
	Intent  intent=new Intent(this,com.example.service.MyService.class);
	startService(intent);
	editor.putBoolean("denglu", true);
	editor.commit();
	finish();
}else {
	Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
			break;
		case R.id.btn_register1:
			name = et_name.getText().toString();
			pass1 = et_password.getText().toString();
			pass2 = et_password2.getText().toString();
			if ((name.length() > 7) && (pass1.length() > 7)
					&& (pass2.length() > 7) && (pass1.equals(pass2))) {
				editor.putString("name", name);
				editor.commit();
				editor.putString("pass1", pass1);
				editor.commit();
				editor.putString("pass2", pass2);
				editor.commit();
				Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
				finish();
			} else {
				Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
	}

}
